package com.shunlin.paopaoku.game;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.physics.box2d.Body;

public class Player {
    private final int ROWS = 4; //图片横着有四张
    private final int COLS = 4; //图片竖着有四张

    public State state = State.IDLE; //人物的状态

    private TextureRegion[] animationList;

    private final float PLAY_SPEED = 0.08f; //动画播放帧之间的间隔
    private final float MOVE_SPEED = 15f; //移动速度

    public final float JUMP = 400; //跳跃高度

    public float x, y;

    public Animation idle; //静止的动画
    public Animation left; //左走的动画
    public Animation right; //右走的动画
    public Animation animation; //动画
    public Body body; //刚体

    public Player(Texture texture, Body body) { //传入一张图片和一个刚体
        this.body = body;

        TextureRegion[][] list = TextureRegion.split(texture, texture.getWidth() / COLS, texture.getHeight() / ROWS); //按照传入的高度和宽度切割图片
        animationList = new TextureRegion[ROWS * COLS];

        int index = 0;

        for (int i = 0; i < list.length; i++) {
            for (int j = 0; j < list[i].length; j++) {
                animationList[index++] = list[i][j];
            }
        }

        init();
    }

	/*public void setX(float x)
	{
		this.x = x;
	}

	public float getX()
	{
		return x;
	}

	public void setY(float y)
	{
		this.y = y;
	}

	public float getY()
	{
		return y;
	}*/

    public enum State { //人物状态的枚举
        IDLE, LEFT, RIGHT
    }

    private void init() {
        idle = new Animation(PLAY_SPEED, new TextureRegion[]{animationList[0]});
        left = new Animation(PLAY_SPEED, new TextureRegion[]{
                animationList[4], animationList[5], animationList[6], animationList[7]
        });
        right = new Animation(PLAY_SPEED, new TextureRegion[]{
                animationList[8], animationList[9], animationList[10], animationList[11]
        });

        idle.setPlayMode(Animation.PlayMode.LOOP); //设置循环播放
        left.setPlayMode(Animation.PlayMode.LOOP);
        right.setPlayMode(Animation.PlayMode.LOOP);
    }
	
	/*public void moveLeft(float wallMin) {
		if((x - MOVE_SPEED) < wallMin) { 
			x = wallMin;
			return ;
		}
		
		x -= MOVE_SPEED;
	}
	
	public void moveRight(float wallMax) {
		if((x + MOVE_SPEED) > wallMax) {
			x = wallMax;
			return ;
		}

		x += MOVE_SPEED;
	}*/

    public void update(float deltaTime) {
        if (state == State.IDLE) {
            animation = idle;
        } else if (state == State.LEFT) {
            animation = left;
        } else if (state == State.RIGHT) {
            animation = right;
        }
    }

    public int getAnimationWidth() {
        return animationList[0].getRegionWidth();
    }

    public int getAnimationHeight() {
        return animationList[0].getRegionHeight();
    }
}
